package top.suven.base.http.admin.order.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.order.model.Order;
import top.suven.base.http.admin.order.service.OrderWebService;
import top.suven.base.http.admin.order.vo.request.OrderRequestVo;
import top.suven.base.http.admin.order.vo.response.OrderResponseVo;
import top.suven.base.http.admin.order.vo.valid.OrderValidGroup;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**   
 * @Title: OrderWebController.java
 * @Description: 订单的控制服务类
 * @author lixiangling
 * @date   2018/08/28 16:02:10
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *订单
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/order/order")
public class OrderWebController {
    private final static Logger logger = LoggerFactory.getLogger(OrderWebController.class);
	@Autowired
	private OrderWebService orderWebService;
    /**
     * @Title: 跳转到订单主界面
     * @Description:orderRequestVo @{Link OrderRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:02:10
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    @RequiresPermissions("order:order_list")
    public String index(){
        return "order/order_index";
    }
    /**
     * @Title: 获取订单分页信息
     * @Description:orderRequestVo @{Link OrderRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:02:10
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    @RequiresPermissions("order:order_list")
    public PageData<OrderResponseVo> list(@Validated(value = {OrderValidGroup.OrderQueryGroup.class}) OrderRequestVo orderRequestVo){
        Order order = new Order();
        BeanUtil.copyProperties(orderRequestVo, order);
        return orderWebService.getOrderPage(order, orderRequestVo.getPage(), orderRequestVo.getLimit());
    }
   /**
    * @Title: 新增订单信息
    * @Description:orderRequestVo @{Link OrderRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "add")
    @RequiresPermissions("order:order_add")
    public ResponseResult add(@Validated(value = {OrderValidGroup.OrderFormGroup.class}) OrderRequestVo orderRequestVo){
        Order order = new Order();
        BeanUtil.copyProperties(orderRequestVo, order);
        order.setStatus(StatusEnum.VALID.getIndex());
        order.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
        orderWebService.addOrder(order);
        return ResponseResult.ok();
    }
    /**
    * @Title: 修改订单信息
    * @Description:orderRequestVo @{Link OrderRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "modify")
    @RequiresPermissions("order:order_modify")
    public ResponseResult modify(@Validated(value = {OrderValidGroup.OrderFormGroup.class}) OrderRequestVo orderRequestVo){
        Order order = new Order();
        BeanUtil.copyProperties(orderRequestVo, order);
        if(order.getOrderId() == null){
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_UPD_INFO_NO_EXIST);
        }
        orderWebService.updOrder(order);
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看订单信息
    * @Description:orderRequestVo @{Link OrderRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    @RequiresPermissions("order:order_list")
    public String detail(ModelMap modelMap, String id){
        Order order = null;
        if(StringUtils.isBlank(id)){
            order = orderWebService.getOrder(id);
        }
        modelMap.put("order", order);
        return "order/order_detail";
    }
    /**
     * @Title: 跳转订单编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:02:10
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    @RequiresPermissions("order:order_modify")
    public String edit(ModelMap modelMap, @RequestParam String id){
        Order order = null;
        if(StringUtils.isBlank(id)){
            order = orderWebService.getOrder(id);
        }
        modelMap.put("order", order);
        return "order/order_edit";
    }
    /**
    * @Title: 跳转订单新增编辑界面
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "newInfo")
    @RequiresPermissions("order:order_add")
    public String newInfo(ModelMap modelMap){
        return "order/order_edit";
    }
    /**
     * @Title: 删除订单信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/28 16:02:10
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    @RequiresPermissions("order:order_del")
    public ResponseResult del( @RequestParam(value = "id") String[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        orderWebService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用订单信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    @RequiresPermissions("order:order_turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") String[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        orderWebService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用订单信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    @RequiresPermissions("order:order_turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") String[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        orderWebService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出订单信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/28 16:02:10
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    @RequiresPermissions("order:order_export")
    public void export(HttpServletResponse response, OrderRequestVo orderRequestVo){
        Order order = new Order();
        BeanUtil.copyProperties(orderRequestVo, order);
        PageData<OrderResponseVo> orderPage = orderWebService.getOrderPage(order, orderRequestVo.getPage(), orderRequestVo.getLimit());
        List<OrderResponseVo> data = orderPage.getData();

        ColumnBox columnBox = ColumnBox.create()
                
			.add("orderId", "订单号")
			.add("padId", "设备ID")
			.add("padCode", "设备编码")
			.add("padGrade", "设备等级")
			.add("goodsId", "商品ID")
			.add("userId", "会员ID即登录帐号")
			.add("orderType", "订单类型,0正常订单、1退款订单")
			.add("orderBizType", "订单业务类型,0购买设备、1充值设备、2充值钱包")
			.add("orderPrice", "订单实付金额")
			.add("realFee", "应付金额")
			.add("realPayAmount", "实付金额(预留)")
			.add("favourableFee", "优惠金额")
			.add("orderStatus", "订单状态（等待买家支付、支付完成）")
			.add("payModeCode", "支付方式编码支付宝、快钱")
			.add("enableStatus", "启用状态")
			.add("onlineTime", "在线时间(天)")
			.add("finishTime", "交易完成时间")
			.add("creater", "创建人")
			.add("createTime", "创建时间")
			.add("modifier", "修改人")
			.add("modifyTime", "修改时间")
			.add("reorder", "排序")
			.add("remark", "备注")
			.add("orderCreateTime", "下单时间")
			.add("rbcAmount", "赠送红豆")
			.add("userSource", "")
			.add("owner", "订单归属，redfinger、baidu")
			.add("customerIp", "用户下单ip")
			.add("goodsName", "商品名称")
			.add("autoRenew", "svip是否自动续期，取字典：1为否，2或空为否");

        try {
            ExportUtils.write(String.format("订单_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}